-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
LFM2 #20797
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
LFM2 #20797
Conversation
…ormers >= 4.54.0.dev0
…usal_conv1d kernel
👋 Hi! Thank you for contributing to the vLLM project. 💬 Join our developer Slack at https://slack.vllm.ai to discuss your PR in #pr-reviews, coordinate on features in #feat- channels, or join special interest groups in #sig- channels. Just a reminder: PRs would not trigger full CI run by default. Instead, it would only run Once the PR is approved and ready to go, your PR reviewer(s) can run CI to test the changes comprehensively before merging. To run CI, PR reviewers can either: Add 🚀 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Summary of Changes
Hello @paulpak58, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
This pull request introduces the LFM2 model architecture into vLLM, including necessary components for hybrid attention and convolution-based layers. It also incorporates changes to handle CUDA versions, transformers library updates, and integration with the vLLM v1 framework.
Highlights
- CMakeLists.txt: Added logic to ignore
nvToolsExt
for CUDA 12.9 by creating an imported interface library if it's not already a target. - vllm/config.py: Modified
get_num_layers_by_block_type
to handle hybrid models, specifically addressing an attribute change intransformers
library versions >= 4.54.0.dev0. It now checks forlayer_types
inhf_text_config
iflayers_block_type
is not found inhf_config
. Also, the logic for summing the block types has been updated to account for 'full_attention' blocks when the block type is 'attention'. - vllm/model_executor/layers/conv.py: Introduced a new
ShortConv
custom operator for implementing a short convolution layer, including forward passes for both native and CUDA execution. The CUDA forward pass includes logic for prefill and decode stages, utilizingcausal_conv1d_fn
andcausal_conv1d_update
respectively. The class also defines aget_state_shape
method. - vllm/model_executor/models/conv_cache.py: Introduced
ConvCacheParams
dataclass andConvCacheManager
class to manage the convolution state cache. TheConvCacheManager
inherits fromConstantSizeCache
and provides methods for copying cache, retrieving tensors for the current run, and providing CUDA graph capture inputs. - vllm/model_executor/models/lfm2.py: Added new modules and classes for the LFM2 model architecture, including
LFM2MLP
,LFM2Attention
,LFM2AttentionDecoderLayer
,LFM2ShortConvDecoderLayer
,LFM2Model
, andLFM2ForCausalLM
. These components define the layers and overall structure of the LFM2 model, supporting hybrid attention and convolution-based layers. TheLFM2ForCausalLM
class integrates the LFM2 model with the vLLM framework, including cache management and logits processing. - vllm/model_executor/models/registry.py: Registered
LFM2ForCausalLM
in the model registry, associating it with the 'lfm2' identifier. - vllm/transformers_utils/configs/ovis.py: Wrapped the
AutoConfig.register
call forAIMv2Config
in a try-except block to prevent errors ifAutoConfig
is not available. - vllm/utils/init.py: Added
conv
to theLayerBlockType
enum. - vllm/v1/attention/backends/mamba_attn.py: Added
get_short_conv_chunk_size
function and updatedMamba2AttentionMetadataBuilder
to supportShortConvSpec
. - vllm/v1/core/single_type_kv_cache_manager.py: Added
ShortConvSpec
to the dictionary of kv cache specs. - vllm/v1/kv_cache_interface.py: Added
ShortConvSpec
dataclass to define the specification for the short convolution KV cache. - vllm/v1/worker/gpu_model_runner.py: Imported
ShortConv
, updatedinitialize_attn_backend
to supportShortConvSpec
, and modified_reshape_kv_cache_tensors
andget_kv_cache_spec
to handle short convolution layers.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command>
or @gemini-code-assist <command>
. Below is a summary of the supported commands.
Feature | Command | Description |
---|---|---|
Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/
folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces support for the LFM2 model, a new hybrid architecture. The changes are comprehensive, affecting model definition, caching mechanisms, and the core engine logic. The implementation cleverly reuses existing infrastructure for Mamba-like layers to handle the new convolution layers, which is a great approach.
I've provided several suggestions to enhance code clarity, maintainability, and robustness. These include refactoring duplicated code, simplifying complex conditions, and improving exception handling. Overall, this is a solid contribution.
This pull request has merge conflicts that must be resolved before it can be |
Signed-off-by: Paul Pak <paulpak58@gmail.com>
if (NOT TARGET CUDA::nvToolsExt) | ||
add_library(CUDA::nvToolsExt INTERFACE IMPORTED) | ||
endif() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
possibly a cleaner solution than this, but this works.
This pull request has merge conflicts that must be resolved before it can be |
Signed-off-by: Paul Pak <paulpak58@gmail.com>
cc @tdoublep - could you take a look at this? |
Signed-off-by: Paul Pak <paulpak58@gmail.com>
Signed-off-by: Paul Pak <paulpak58@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work! My general impression is that this is a pretty clean integration. I've left some comments after an initial pass.
self.state_indices_tensor) | ||
|
||
|
||
class ConvCacheManager(ConstantSizeCache): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this class needed for anything other than V0 support? If so, I question whether we should include it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah only v0. Do you have an estimate for full v0 deprecation?
Signed-off-by: Paul Pak <paulpak58@gmail.com>
Thanks @tdoublep -- appreciate the fast turnaround. All comments are addressed. I haven't removed |
Signed-off-by: Paul Pak <paulpak58@gmail.com>
@@ -89,11 +89,17 @@ class Mamba2AttentionMetadataBuilder( | |||
|
|||
def __init__(self, kv_cache_spec: AttentionSpec, vllm_config: VllmConfig, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
kv_cache_spec
is not really an AttentionSpec here. It's a StaticCacheSpec but for us to resolve mypy issues, we'd have to do something like the following:
M = TypeVar("M")
S = TypeVar("S", bound="AttentionSpec")
class AttentionMetadataBuilder(Generic[M, S], abc.ABC):
class Mamba2AttentionMetadataBuilder(
AttentionMetadataBuilder[Mamba2AttentionMetadata, StaticCacheSpec]):
It's not messy, but I just found it quicker to label kv_cache_spec
as an AttentionSpec to make mypy happy and then check the type in the init.
This pull request has merge conflicts that must be resolved before it can be |
Signed-off-by: Paul Pak <paulpak58@gmail.com>
Signed-off-by: Paul Pak <paulpak58@gmail.com>
Signed-off-by: Paul Pak <paulpak58@gmail.com>
Both eager & torch-compile passes internal tests. |
Essential Elements of an Effective PR Description Checklist
supported_models.md
andexamples
for a new model.Purpose
This PR implements LFM2.
Implementation Details
These changes are relatively lightweight & independent. Some components were modified to generalize the Mamba2 V1 modules to handle for both MambaSpec’s and ShortConvSpec’s, e.g.
HybridAttentionMambaModelConfig
->HybridAttentionStaticCacheModelConfig
, which will be needed for other V1 helpers (e.g. MambaManager) once the horizon of static cache/fixed-state layers increases. For simplicity, in most cases, I try to use the same V1 modules to handle for both Mamba2 and ShortConv layers.Test Plan
See
test_hybrid.py
.Test Result
See
test_hybrid.py
. Basic hybrid tests, batching, chunked_prefill passes. Later tests don't pass due to scheduling incompatibilities with hybrids (model-agnostic).Need transformers
v.4.54.0.dev0
(install from source). This version doesn't supporttoken_type_ids
which is populated in HfRunner. I did not change this in the PR but if you want to test the hybrid V1 path locally intest_hybrid.py
, you’ll have to pop those kwargs from wrapped_inputs inconftest.py
before passing them through the HF model.(Optional) Documentation Update
Updates to
supported_models.md
andv1_guide.md
.